package com.weyoung.app.config;

/**
 * @author qinbo
 * @version 1.0
 * @description: TODO
 * @date 2025/4/25 11:23
 */
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesisParam;
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesizer;
import com.alibaba.dashscope.audio.ttsv2.enrollment.Voice;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.weyoung.app.controller.FileController;
import com.weyoung.common.utils.UpLoadFiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import static java.lang.System.exit;

public class VoiceEnrollmentSampleCodes {
    public static String apiKey = "sk-b016b75c59d840f7b98c1865f4f7ca42";
    private  static String fileUrl = "https://bo-1327186976.cos.ap-guangzhou.myqcloud.com/file/661a7273-0588-4bda-aa6a-29fb979bb730.m4a";  // 请按实际情况进行替换
    private static String prefix = "one4";
    private static String targetModel = "cosyvoice-v2";
    public static byte[] deal(String text)
            throws Exception {
        // 复刻声音
        VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
        Voice myVoice = service.createVoice(targetModel, prefix, fileUrl);
        System.out.println("RequestId: " + service.getLastRequestId());
        System.out.println("your voice id is " + myVoice.getVoiceId());
        // 使用复刻的声音来合成文本为语音
        SpeechSynthesisParam param = SpeechSynthesisParam.builder()
                .apiKey(apiKey)
                .model(targetModel)
                .voice(myVoice.getVoiceId())
                .build();
        SpeechSynthesizer synthesizer = new SpeechSynthesizer(param, null);
        ByteBuffer audio = synthesizer.call(text);
        // 保存合成的语音到文件
        System.out.println("TTS RequestId: " + synthesizer.getLastRequestId());

        return audio.array();
    }
}
